package model;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Actor {
		private int acid;
		private String ac_name;
		private String ac_bio;
		public Actor(){}
		public Actor(int acid, String ac_name, String ac_bio) {
			this.acid = acid;
			this.ac_name = ac_name;
			this.ac_bio = ac_bio;
		}
		
		public int getAcid() {
			return acid;
		}
		public void setAcid(int acid) {
			this.acid = acid;
		}
		public String getAc_name() {
			return ac_name;
		}
		public void setAc_name(String ac_name) {
			this.ac_name = ac_name;
		}
		public String getAc_bio() {
			return ac_bio;
		}
		public void setAc_bio(String ac_bio) {
			this.ac_bio = ac_bio;
		}
		public int getSizeDB(){
			int result = 0;
			PreparedStatement sqlu = null;
			Connection con = new ConnectDB().getConnection();
			try {
				sqlu = con
						.prepareStatement("select COUNT(acid) from actors");
				ResultSet rs = sqlu.executeQuery();
				while (rs.next()) {
					return rs.getInt(1);
				}
				sqlu.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return result;
		}
		public String checkName() {
			String name = this.ac_name;
				if(name == null){
					return "Bạn chưa nhập Actor";
				}else{
					name =name.trim();
					if (name.equals("")) {
						return "Bạn chưa nhập Actor";
					}else{
						PreparedStatement sqlp = null;
						Connection connection = new ConnectDB().getConnection();
						try {
							sqlp = connection.prepareStatement("SELECT * FROM actors WHERE ac_name = ?");
							sqlp.setString(1, name);
							ResultSet rs = sqlp.executeQuery();
							while(rs.next() == true){
								return "Actor đã tồn tại.";
							}
						
							connection.close();
						} catch (Exception e) {
							e.printStackTrace();
						}	
						
					}
				}
			return "";
		}
		public String checkNewName() {
			String name = this.ac_name;
				if(name == null){
					return "Bạn chưa nhập Actor";
				}else{
					name =name.trim();
					if (name.equals("")) { return "Bạn chưa nhập Actor";}
					else if(this.getAcByName(name).getAcid()!=this.getAcid()){
						return "Actor đã tồn tại. ";
				}
				}
				return "";
		}
		public boolean Create() {
			PreparedStatement sqlr = null;
			boolean result = false;

			Connection con = new ConnectDB().getConnection();
			try {
				sqlr = con
						.prepareStatement("INSERT INTO actors( ac_name, ac_bio) VALUES (? , ?)");
				
				sqlr.setString(1, this.ac_name);
				sqlr.setString(2, this.ac_bio);
				int number = sqlr.executeUpdate();
				if (number > 0) {
					result = true;
				}
				sqlr.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return result;
		}
		public boolean upDate() {
			PreparedStatement sqlr = null;
			boolean result = false;

			Connection con = new ConnectDB().getConnection();
			try {
				sqlr = con
						.prepareStatement("UPDATE actors SET  ac_name = ?, ac_bio = ? WHERE acid = ? LIMIT 1");
				sqlr.setString(1, this.getAc_name());
				sqlr.setString(2, this.getAc_bio());
				sqlr.setInt(3, this.acid);
				int number = sqlr.executeUpdate();
				if (number > 0) {
					result = true;
				}
				sqlr.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return result;
		}
		public Actor getActor(int acid){
			Actor actor = null;
			PreparedStatement sqlp = null;
			Connection connection = new ConnectDB().getConnection();
			try {
				sqlp = connection.prepareStatement("SELECT * FROM actors WHERE acid = ?");
				sqlp.setInt(1, acid);
				ResultSet rs = sqlp.executeQuery();
				while(rs.next() == true){
					int acid1 = rs.getInt(1);
					String ac_name = rs.getString(2);
					String ac_bio = rs.getString(3);
					actor = new Actor(acid1, ac_name, ac_bio);
				}
				sqlp.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return actor;
		}
		public Actor getAcByName(String acname) {
			acname = (acname == null) ? this.getAc_name() : acname;
			PreparedStatement sqll = null;
			Actor result = null;
			Connection c = new ConnectDB().getConnection();
			try {
				sqll = c.prepareStatement("select * from actors where ac_name = ?");
				sqll.setString(1, acname);
				ResultSet rs = sqll.executeQuery();
				while (rs.next() == true) {
					int acid1 = rs.getInt(1);
					String ac_name = rs.getString(2);
					String ac_bio = rs.getString(3);
					result = new Actor(acid1, ac_name, ac_bio);
				}
				sqll.close();
				c.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			return result;
		}
		public int[] getAcID(String orby /*n_name */){
			if(orby==null){
				orby="ac_name";
			}else{
				orby=orby.trim();
				if (!((orby.equals("acid"))||(orby.equals("ac_name"))||(orby.equals("ac_bio")) )) {
					orby="ac_name";
				}
			}
			int[] result = new int[this.getSizeDB()];
			int i =0;
			PreparedStatement sqlp = null;
			Connection connection = new ConnectDB().getConnection();
			try {
				sqlp = connection.prepareStatement("SELECT acid FROM actors ORDER BY "+ orby + " ASC  ");
				ResultSet rs = sqlp.executeQuery();
				while(rs.next() == true){
					result[i] = rs.getInt(1);
					i++;
				}
				sqlp.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result;
		}
		public String toString (){
			return this.ac_bio + " " + this.ac_name + " " +  this.ac_bio ;
		}
		public boolean isAcID(String acid) throws IOException{ /*kiem tra tinh hop le cua nid va co nam trong DB hay khong*/
			if(acid==null) return false;
			if(acid.trim().equals(""))return false;
			acid.trim();
			int id = -1;
			try {
				id = Integer.parseInt(acid);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			PreparedStatement sqlp = null;
			Connection connection = new ConnectDB().getConnection();
			try {
				sqlp = connection.prepareStatement("SELECT acid FROM actors where acid= ? ");
				sqlp.setInt(1, id);
				ResultSet rs = sqlp.executeQuery();
				while (rs.next() == true) {
					return true;
				}
				sqlp.close();
				connection.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
		public boolean del(int acid){
			PreparedStatement delpd = null;
			Connection con = new ConnectDB().getConnection();
			try {
				delpd = con.prepareStatement("DELETE FROM actors WHERE acid = ?  ");
				delpd.setInt(1, acid);
				int number = delpd.executeUpdate();
				if (number > 0) {
					return true;
				}
				delpd.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return  false;
		}
}
